function getD2rUrl(){
    var d2r = localStorage.getItem('d2r');
    if(d2r>0){
        return "http://midownload.sinaapp.com/d2r/"
    }else{
        return "https://d.miwifi.com/d2r/"
    }
}
/*注释转字符串*/
function funString(f) {　
    return f.toString().replace(/^[^\/]+\/\*!?\s?/, '').replace(/\*\/[^\/]+$/, '');
}
/*插入css*/
function insertStyle(styles,styleId){
var style = document.createElement("style");
style.id = styleId?styleId:'';
(document.getElementsByTagName("head")[0] || document.body).appendChild(style);
style.appendChild(document.createTextNode(styles));
}
/*获取该页面的uri*/
function getURI(){
	return window.location.origin+window.location.pathname+window.location.search;
}
var mi = {
	/*通过连接下载*/
	download_url:function(_href,_name,_src) {
		var href = getD2rUrl()+"?url=" + Base64.encodeURI(_href) + "&src="+encodeURIComponent(_src)+'&name='+encodeURIComponent(_name)
    	window.open(href);
	},
	/*通过拦截事件下载*/
	/*推荐设置src 用于判断是否是来自该页面的连接 防止误下载*/
	download_event:function(_name,_src,_fun){
		chrome.extension.sendRequest({type:'fun',fun:'xmd',data:[_name,_src]}, _fun);
	}
}



insertStyle(funString(function(){/*
.slide-show-header h2 {
width: 250px !important;
}
#MiDownFileButtom{
	border: 1px #ff6000 solid;
padding-left: 0;
background: -webkit-linear-gradient(top,#ff8000,#ff6000);
height: 30px;
line-height: 30px;
margin:4px 0;
margin-right: 10px; 
border-radius:3px;
}
#MiDownFileButtom:active{
	background: -webkit-linear-gradient(top,#ff6000,#ff8000);
}
#MiDownFileButtom b{
	padding: 0 10px;
	background: none;
	height: 30px;
line-height: 30px;
color:#fff;
}

.down-xm{
	-webkit-filter: hue-rotate(170deg) saturate(2);
}
*/}));



var viewurl = "http://pan.baidu.com/wap/view?fsid=1115511758543623"



var btn = '<a class="new-dbtn" hidefocus="true" href="javascript:;" id="MiDownFileButtom">'+
'<b>下载到小米路由</b>'+
'</a>';
var btn2 = '<a node-type="btn-item" data-key="download" class="btn download down-xm" href="javascript:;"></a>';
var btn3= '<li style="display: list-item;" class="rcm-xmd"><a hidefocus="true" href="javascript:;" class="list-node" node-type="item download" data-key="download">下载到小米</a></li>';

src = getURI();
$(function(){
	$('.item').on('mouseover',function(){
		var extname = $(this).attr('data-extname');
		if(extname!='dir' && extname!='shoucang' && extname!='yingyong'){
			if($(this).find('.down-xm').length==0){
				$(this).find('.download').after(btn2);
				$(this).find('.down-xm').click(function(){
					mi.download_event($(this).find('.name').attr('title'),src,function(){});
				});
			}
		}else{
			$(this).find('.down-xm').remove();
		}
	});
    $('body').mouseup(function(){
        setTimeout(function(){
            if($('.rcm-xmd').length==0&&$('.menu-main').length!=0){
                $('.menu-main').append(btn3);
                $('.rcm-xmd').click(function(){
                    mi.download_event('',src,function(){});
                });
            }
        },200);
    });
});





$('#downFileButtom').after(btn);
var downFileButtomname = $('.slide-show-header .ellipsis').text();
$('#MiDownFileButtom').click(function(){
	mi.download_event(downFileButtomname,src,function(){$("#downFileButtom").get(0).click();});
})